package com.jxnd.sunflower.controller;

import com.jxnd.sunflower.common.JSONResult;
import com.jxnd.sunflower.domain.RGoods;
import com.jxnd.sunflower.domain.ROrder;
import com.jxnd.sunflower.mapper.ROrderMapper;
import com.jxnd.sunflower.service.AlipayService;
import com.jxnd.sunflower.service.GoodsService;
import com.jxnd.sunflower.utils.JwtToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author: 敷衍
 * @description: 支付相关 因为支付宝沙箱太不稳定 不好调试
 * @date: 2021/12/8 20:04
 * @version: 1.0
 */
@Api(value = "支付",tags = "支付")
@RestController
@RequestMapping("/payment")
public class PaymentController {


    @Autowired
    AlipayService alipayService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ROrderMapper mapper;


    @ApiOperation(value = "支付宝支付",notes = "支付宝支付")
    @RequestMapping("/pay")
    public String payMent(HttpServletResponse response, HttpServletRequest request) {
        String str="" ;
        try {
            str = alipayService.aliPay(response,request);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return str;
    }


    @ApiOperation(value = "支付",notes = "支付")
    @RequestMapping("/pays")
    public JSONResult pay(@RequestHeader("Authorization") String token, Integer goodsId) {
        int userId = JwtToken.getUserId(token);
        RGoods rGoods = goodsService.queryInfo(goodsId);
        ROrder order = new ROrder();
        order.setGoodsId(goodsId);
        order.setUserId(userId);
        order.setOrderNo(System.currentTimeMillis());
        order.setPayment(rGoods.getPrice());

        int insert = mapper.insert(order);


        return JSONResult.ok("支付成功");


    }
}
